package xyz.xtt.pay.mq;

import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import xyz.xtt.pay.dto.PayDelayMessage;
import xyz.xtt.pay.service.INftOrderService;

/**
 * @author dale
 * @date 2025/1/13
 */
@Slf4j
@Component
@RefreshScope
@RocketMQMessageListener(topic = "${rocketmq.producer.paydealy}",selectorExpression="PayOrderDelay" ,consumerGroup = "${rocketmq.consumer.group.paydealy}")
public class PayDelayConsumer implements RocketMQListener<String> {
	@Autowired
	private INftOrderService nftOrderService;

	@Value("${rocketmq.producer.paydealy}")
	private String topic;

	@Override
	public void onMessage(String message) {
		if (StringUtils.isEmpty(message)) {
			return;
		}
		boolean isJson = JSONUtil.isTypeJSON(message);
		if (!isJson) {
			log.warn("PayDelayConsumer topic:{} Received message: {} is not json", topic, message);
			return;
		}else {
			log.info("PayDelayConsumer topic:{} Received message: {}", topic, message);
		}

		PayDelayMessage payDelayMessage = JSONUtil.toBean(message, PayDelayMessage.class);
		if (Objects.isNull(payDelayMessage)) {
			log.info("PayDelayConsumer topic:{} JSONUtil.toBean is null", topic);
			return;
		}
		try {
			nftOrderService.payDelayCancel(payDelayMessage);
		} catch (Exception e) {
			log.info("PayDelayConsumer ex:{}", e.getMessage(), e);
			throw new RuntimeException("PayDelayConsumer Consumer Message exceotion");
		}
	}
}
